<?php

/**
 * This is the model class for table "{{invoice}}".
 *
 * The followings are the available columns in table '{{invoice}}':
 * @property integer $invoice_id
 * @property integer $customer_id
 * @property integer $address_id
 * @property string $status
 * @property string $date
 * @property double $cashback
 * @property double $discount
 *
 * The followings are the available model relations:
 * @property Customer $customer
 * @property Purchase[] $purchases
 */
class Invoice extends CActiveRecord
{



	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Invoice the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{invoice}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
/* 			array('customer_id, status, date, cashback, discount', 'required'), */
			array('customer_id', 'numerical', 'integerOnly'=>true),
			array('cashback, discount', 'numerical'),
			array('status', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('invoice_id, customer_id, address_id, status, date, cashback, discount', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'customer' => array(self::BELONGS_TO, 'Customer', 'customer_id'),
			'purchases' => array(self::HAS_MANY, 'Purchase', 'invoice_id'),
			'addresses' => array(self::BELONGS_TO, 'Address', 'address_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'invoice_id' => 'Rechnungsnummer',
			'customer_id' => 'Kundennummer',
			'address_id' => 'Adresse',
			'status' => 'Status',
			'date' => 'Rechnungsdatum',
			'cashback' => 'Skonto',
			'discount' => 'Rabatt',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('invoice_id',$this->invoice_id);
		$criteria->compare('customer_id',$this->customer_id);
		$criteria->compare('address_id', $this->address_id);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('cashback',$this->cashback);
		$criteria->compare('discount',$this->discount);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    protected function afterFind ()
    {
        // convert to display format
        $this->date = strtotime($this->date);
        $this->date = date ('d.m.Y', $this->date);

        parent::afterFind ();
    }

    protected function beforeValidate ()
    {
        // convert to storage format
        $this->date = strtotime ($this->date);
        $this->date = date ('Y-m-d', $this->date);

        return parent::beforeValidate ();
    }
}